var menuCtrl = new XFApp.client();
XFApp.client.prototype.menu = {
    isSubmitting : false,
    successCode : 10000,
    menuList : {},
    urlItems : {
        menuListUrl : basePath + "/menu/ajax/list",
        saveUrl : basePath + "/menu/ajax/saveOrUpdate",
        deleteUrl : basePath + "/menu/ajax/delete"
    },
    initMenuList : function(list){
        var _this = this;
        if(!list){
            return;
        }
        $.each(list, function(id,menu){
            var key = menu.id;
            _this.menuList[key] = menu;
            if(menu.subMenus && menu.subMenus.length > 0){
               _this.initMenuList(menu.subMenus);
            }
        });
    },
    clearMenuForm : function(){
        $("#description").val("");
        $("#name").val("");
        $("#menu_id").val("");
        $("#parentName").val("");
        $("#parentId").val("");
        $("#resourceType").val(1);
        $("#resourceUrl").val("");
        $("#sort").val("");
        $("#icon").val("");
    },
    renderMenu : function(menuId){
        this.clearMenuForm();
        var node = this.menuList[menuId];
        var parentNode =this.menuList[node.parentId];
        if(!parentNode){
            parentNode = {
                description : "菜单管理",
                id : -1
            }
        }
        $("#description").val(node.description);
        $("#name").val(node.name);
        $("#menu_id").val(node.id);
        $("#parentName").val(parentNode.description);
        $("#parentId").val(parentNode.id);
        $("#resourceType").val(node.resourceType.index);
        $("#resourceUrl").val(node.resourceUrl);
        $("#sort").val(node.sort);
        $("#icon").val(node.icon);
    },
    loadMenus : function(){
        var _this = this;
        $.ajax({
            url:_this.urlItems.menuListUrl,
            dataType:'json',
            type:'post',
            success:function(data){
                if(data.code != menuCtrl.menu.successCode){
                    webDialogUtil.simpleAlert(data.msg);
                }else{
                    var $expandibleTree = $('#treeview-expandible').treeview({
                        data: data.result,
                        levels : 2,
                        nodesName : 'subMenus',
                        text : 'description',
                        nodeIdName : 'id',
                        onNodeCollapsed: function(event, node) {
                            $('#expandible-output').prepend('<p>' + node.text + ' was collapsed</p>');
                        },
                        onNodeExpanded: function (event, node) {
                            $('#expandible-output').prepend('<p>' + node.text + ' was expanded</p>');
                        }
                    });

                    //初始化菜单数据到menuList
                    _this.initMenuList(data.result);

                    $("#treeview-expandible").on("click",function(e){
                        if(e.target && e.target.nodeName.toLowerCase() == "li") {
                            var _obj = $(e.target);
                            _this.renderMenu(_obj.attr("data-id"));
                        }
                    });
                }
            },
            error:function(){
                webDialogUtil.simpleAlert("出现异常,请联系开发人员");
            }
        });
    },
    saveDialog : function(){
        webDialogUtil.simpleConfrimAlert("确认保存", function(){menuCtrl.menu.saveMenu(id);});
    },
    saveMenu : function(){
        $.ajax({
            url:this.urlItems.saveUrl,
            data:$('#menu_edit').serialize(),
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                webDialogUtil.simpleAlert(data.msg);
                //刷新一下菜单
                menuCtrl.menu.loadMenus();
            },
            error : function() {
                webDialogUtil.simpleAlert("删除菜单失败，请刷新后重试!");
            }
        });
    },
    createMenu: function () {
        //先判断是否选中了父节点，没父节点不能新增
        var _selectedNode = $("#treeview-expandible li.node-selected").first();
        if(_selectedNode.length < 1){
            webDialogUtil.simpleAlert("请先选择父节点，再新建菜单！");
            return;
        }
        var parentNode = this.menuList[_selectedNode.attr("data-id")];
        this.clearMenuForm();
        $("#parentName").val(parentNode.description);
        $("#parentId").val(parentNode.id);
        $("#resourceType").val(1);
    },
    deleteMenuDialog : function(){
        //先判断是否选中了节点，没选中不能删除
        var _selectedNode = $("#treeview-expandible li.node-selected").first();
        if(_selectedNode.length < 1){
            webDialogUtil.simpleAlert("请先选择要删除的菜单！");
            return;
        }
        var nodeId = _selectedNode.attr("data-id");
        if(nodeId < 1){
            webDialogUtil.simpleAlert("根菜单不能删除，请选择其他菜单删除!");
            return;
        }
        var node = this.menuList[_selectedNode.attr("data-id")];
        if(node.subMenus && node.subMenus.length > 0){
            webDialogUtil.simpleAlert("请先删除子菜单，再删除父菜单!");
            return;
        }
        webDialogUtil.simpleConfrimAlert("确定要删除此菜单？删除后不可恢复！", function(){menuCtrl.menu.deleteMenu(nodeId);});
    },
    deleteMenu : function(nodeId){
        $.ajax({
            url:this.urlItems.deleteUrl,
            data:{menuId : nodeId},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {

                webDialogUtil.simpleAlert(data.msg);
                //刷新一下菜单
                menuCtrl.menu.loadMenus();
            },
            error : function() {
                webDialogUtil.simpleAlert("操作失败!");
            }
        });
    },
    event : function(){
        $('#nestable-menu').on('click', function(e){
            var target = $(e.target),
                action = target.data('action');
            if (action === 'expand-all') {
                $('.dd').nestable('expandAll');
            }
            if (action === 'collapse-all') {
                $('.dd').nestable('collapseAll');
            }
        });
        menuCtrl.menu.loadMenus();

        $("#save_menu").on("click", function(){
            menuCtrl.menu.saveMenu();
        });
        $("#create_menu").on("click", function(){
            menuCtrl.menu.createMenu();
        });
        $("#delete_menu").on("click", function(){
            menuCtrl.menu.deleteMenuDialog();
        });

    }
}

$(function(){
    menuCtrl.menu.event();
});